#ifdef KITSCHY_DEBUG_MEMORY 
#include "debug_memorymanager.h"
#endif

#ifdef WIN32
#include "windows.h"
#endif

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "math.h"
#include "pthread.h"

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

#include "BList.h"
#include "BVector.h"
#include "geometrics.h"
#include "quaternion.h"

#include "symbol.h"
#include "sort.h"
#include "ontology.h"
#include "featureterm.h"
#include "FTKBase.h"

#include "myglutaux.h"
#include "FTGLPolygonFont.h"
#include "AWLVP_DistanceMatrix.h"
#include "AWLocalViewport.h"
#include "AWLVP_NOOSTextFTerm.h"

#include "AWEntity.h"
#include "AWMessage.h"
#include "AWBody.h"
#include "AWMsgMoveBody.h"
#include "AWNOOSCBBody.h"

#include "CaseBaseUtils.h"

#include "loader.h"


extern int SCREEN_DX,SCREEN_DY;

AW_LVP_NOOSTextFTerm::AW_LVP_NOOSTextFTerm(int x,int y,int dx,int dy,FeatureTerm *ft,FTKBase *dm) : AW_LocalViewport(x,y,dx,dy)
{
	m_font=0;

	m_ft=ft;
	m_domainmodel=dm;

	m_zoom=1;
	m_desired_zoom=1;

	m_center_x=0;
	m_center_y=0;

	if (m_ft!=0) {
		char *s=m_ft->toStringNOOS(dm),*s2,*s3;
		char *tmp;

		s3=s2=s;
		while(*s2!=0) {
			if (*s2=='\n' || *s2=='\r') {
				char c=*s2;
				*s2=0;
				tmp=new char[strlen(s3)+1];
				strcpy(tmp,s3);
				m_lines.Add(tmp);
				*s2=c;
				while(*s2=='\n' || *s2=='\r') s2++;
				s3=s2;
			} else {
				s2++;
			} //
		} // while 		
		if (s3!=s2) {
			tmp=new char[strlen(s3)+1];
			strcpy(tmp,s3);
			m_lines.Add(tmp);
		} // if 
	} /* if */ 

} /* AW_LVP_NOOSTextFTerm::AW_LVP_NOOSTextFTerm */ 


AW_LVP_NOOSTextFTerm::~AW_LVP_NOOSTextFTerm()
{
	m_ft=0;
	m_domainmodel=0;

#ifndef KITSCHY_DEBUG_MEMORY 
	// I don't know why, but the memory_manager complains about this free:
	if (m_font!=0) delete m_font;
#endif
} /* AW_LVP_NOOSTextFTerm::~AW_LVP_NOOSTextFTerm */ 



void AW_LVP_NOOSTextFTerm::draw_internal(void)
{
	float f=1.0F;
	int vpx=0,vpy=0,vpw=800,vph=600;

	if (m_font==0) {
		FILE *fp=fopen("arial.ttf","r");
		if (fp!=0) {
			fclose(fp);
			m_font=new FTGLPolygonFont("arial.ttf");
			m_font->FaceSize(2);
			m_font->Depth(0.2F);
		} else {
			return;
		} /* if */ 
	} /* if */ 

	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	glLoadIdentity();

	{
		int tmp[4];
		glGetIntegerv(GL_VIEWPORT,tmp);
		vpx=tmp[0];
		vpy=tmp[1];
		vpw=tmp[2];
		vph=tmp[3];
		f=float(vpw)/float(vph);
	}

	glDisable(GL_DEPTH_TEST);

	/* blended border: */ 
	{
		int i;
		float f;

		glEnable(GL_BLEND);
		for(i=0;i<10;i++) {
			f=1-(i*0.01F);
			glColor4f(0,0,0,0.2F);
			glBegin(GL_QUADS);
			glVertex3f(-f,-f,0);
			glVertex3f(f,-f,0);
			glVertex3f(f,f,0);
			glVertex3f(-f,f,0);
			glEnd();
		} /* for */ 
		glDisable(GL_BLEND);
	}

	if (m_ft!=0) {

		if (m_zoom<m_desired_zoom) {
			if (m_zoom*1.075F>=m_desired_zoom) m_zoom=m_desired_zoom;
										 else m_zoom*=1.075F;
		} /* if */ 

		if (m_zoom>m_desired_zoom) {
			if (m_zoom/1.075F<=m_desired_zoom) m_zoom=m_desired_zoom;
										 else m_zoom/=1.075F;
		} /* if */ 

		// Draw the text:
		if (m_font!=0) {
			char *str;

			glPushMatrix();
			glTranslatef(-0.9,0.9,0);

			glTranslatef(-m_center_x/10,-m_center_y/10,0);
			glScalef(m_zoom*1.75,m_zoom*1.75,m_zoom*1.75);
//			glTranslatef(-(bbox[0]+bbox[2])/2,-(bbox[1]+bbox[3])/2,0);
			glScalef(0.01F,0.01F,0.01F);

			m_lines.Rewind();
			while(m_lines.Iterate(str)) {
				glColor3f(1,1,1);
				glPushMatrix();
				m_font->Render(str);
				glPopMatrix();
				glTranslatef(0,-2,0);
			} /* while */ 

			glPopMatrix();
		} /* if */ 

		/* Close button: */ 
		{
			glPushName(1);
			glPushMatrix();
			glTranslatef(0.7F,-0.85F,0);
			glColor3f(0.4F,0.6F,1.0F);
			glutSolidBox(0.20F,0.06F,0.1F);
			glTranslatef(0,0,-0.2F);
			{
				float llx,lly,llz,urx,ury,urz;
				m_font->BBox( "Close", llx, lly, llz, urx, ury, urz);		
				glPushMatrix();
				glScalef(0.03F,0.03F,0.03F);
				glTranslatef(-(urx-llx)/2,-0.5F,0);
				glColor3f(1,1,1);
				m_font->Render("Close");
				glPopMatrix();
			}
			glPopMatrix();
			glPopName();
		}

		/* Zoom in/out: */ 
		{
			glPushName(2);
			glPushMatrix();
			glTranslatef(0.33F,-0.775F,0);
			glColor3f(0.4F,0.6F,1.0F);
			glutSolidBox(0.075F,0.05F,0.1F);
			glTranslatef(0,0,-0.2F);
			{
				float llx,lly,llz,urx,ury,urz;
				m_font->BBox( "In", llx, lly, llz, urx, ury, urz);		
				glPushMatrix();
				glScalef(0.03F,0.03F,0.03F);
				glTranslatef(-(urx-llx)/2,-0.5F,0);
				glColor3f(1,1,1);
				m_font->Render("In");
				glPopMatrix();
			}
			glPopMatrix();
			glPopName();
		}
		{
			glPushName(3);
			glPushMatrix();
			glTranslatef(0.33F,-0.925F,0);
			glColor3f(0.4F,0.6F,1.0F);
			glutSolidBox(0.075F,0.05F,0.1F);
			glTranslatef(0,0,-0.2F);
			{
				float llx,lly,llz,urx,ury,urz;
				m_font->BBox( "Out", llx, lly, llz, urx, ury, urz);		
				glPushMatrix();
				glScalef(0.03F,0.03F,0.03F);
				glTranslatef(-(urx-llx)/2,-0.5F,0);
				glColor3f(1,1,1);
				m_font->Render("Out");
				glPopMatrix();
			}
			glPopMatrix();
			glPopName();
		}

		/* Arrows: */ 
		{
			glPushName(4);
			glColor3f(0.4F,0.6F,1.0F);
			glBegin(GL_TRIANGLES);
			glVertex3f(-0.6,-0.7,-0.1);
			glVertex3f(-0.5,-0.8,-0.1);
			glVertex3f(-0.6,-0.9,-0.1);
			glEnd();		
			glPopName();

			glPushName(5);
			glColor3f(0.4F,0.6F,1.0F);
			glBegin(GL_TRIANGLES);
			glVertex3f(-0.6,-0.9,-0.1);
			glVertex3f(-0.7,-1.0,-0.1);
			glVertex3f(-0.8,-0.9,-0.1);
			glEnd();		
			glPopName();

			glPushName(6);
			glColor3f(0.4F,0.6F,1.0F);
			glBegin(GL_TRIANGLES);
			glVertex3f(-0.8,-0.9,-0.1);
			glVertex3f(-0.9,-0.8,-0.1);
			glVertex3f(-0.8,-0.7,-0.1);
			glEnd();		
			glPopName();

			glPushName(7);
			glColor3f(0.4F,0.6F,1.0F);
			glBegin(GL_TRIANGLES);
			glVertex3f(-0.8,-0.7,-0.1);
			glVertex3f(-0.7,-0.6,-0.1);
			glVertex3f(-0.6,-0.7,-0.1);
			glEnd();		
			glPopName();

		}
	} /* if */ 

	glEnable(GL_DEPTH_TEST);

	glPopMatrix();

} /* AW_LVP_NOOSTextFTerm::draw_internal */ 



bool AW_LVP_NOOSTextFTerm::MouseClick_internal(int selected)
{

	switch(selected) {
	case 1: m_ft=0;
			break;
	case 2: /* zoom in */ 
			if (m_desired_zoom<16) m_desired_zoom*=1.25;	
			if (m_desired_zoom>=16) m_desired_zoom=16;
			break;

	case 3: /* zoom out: */ 
			if (m_desired_zoom>1) m_desired_zoom/=1.25;
			if (m_desired_zoom<=1) m_desired_zoom=1;
			break;
	case 4: /* right: */ 
			m_center_x+=1;
			break;
	case 5: /* down: */ 
			m_center_y-=1;
			break;
	case 6: /* left: */ 
			m_center_x-=1;
			break;
	case 7: /* up: */ 
			m_center_y+=1;
			break;
	default:
			break;
	} /* switch */ 
	
	return true;
} /* AW_LVP_NOOSTextFTerm::MouseClick */ 


FeatureTerm *AW_LVP_NOOSTextFTerm::get_ft(void)
{
	return m_ft;
} /* AW_LVP_NOOSTextFTerm::get_ft */ 

